import java.sql.* ;

class SalesOrder
{
	CreateConnection cc ;

	int sales_id ;  // index-1 in both tables sales_invoice and sales_invoice_details
	int customer_id ; // index-2 in sales_invoice
	String invoice_number ; // index-3 in sales_invoice

	java.sql.Date date ; // index-4 in sales_invoice
	java.sql.Time time ; // index-5 in sales_invoice

	float total_amount ; // index-6 in sales_invoice


// As for one sales_id - there will be  a selling to one customer with many products, so we will need to insert details
// of all products in Sales_Invoice_details table for a sales_id.


	String[] product_id ; // index-2 in sales_invoice_details
	String[] quantity ; //index-3 in sales_invoice_details

	String[] price ; //index-4 in sales_invoice_details
	String[] VAT ;// index-5 in sales_invoice_details








	SalesOrder()
	{

		try
		{

			cc = new CreateConnection() ;

		}
		catch(Exception e)
		{

		}


	}




SalesOrder(String[] s, String[] product_id, String[] quantity, String[] price, String[] VAT )
	{
		sales_id = Integer.parseInt(s[0]) ;
		customer_id = Integer.parseInt(s[1]) ;
		invoice_number = s[2] ;


		date = java.sql.Date.valueOf(s[3]) ;  // will convert "2012-09-12" type string into java.sql.Date object
		time = 	java.sql.Time.valueOf(s[4]) ;
		total_amount = Float.parseFloat(s[5]) ;
/*
		product_id = Integer.parseInt(s[5]) ;
		quantity = Integer.parseInt(s[6]) ;
		price = Float.parseFloat(s[7]) ;
		VAT = Float.parseFloat(s[8]) ;

*/

		this.product_id = product_id ;
		this.quantity = quantity;
		this.price = price ;
		this.VAT = VAT;

/*
		System.out.println(sales_id);
		System.out.println(customer_id);
		System.out.println(invoice_number);
		System.out.println(date);
		System.out.println(time);
		System.out.println(product_id);
		System.out.println(quantity);
		System.out.println(price);
		System.out.println(VAT);
*/


		try
		{

			cc = new CreateConnection() ;

		}
		catch(Exception e)
		{

		}


	}





	void insertDetailsInSalesInvoice()
	{

		try
		{

			String sql1 = "Insert into sales_invoice values(?,?,?,?,?,?)" ;

			PreparedStatement ps1 = cc.getStatement(sql1) ;

			ps1.setInt(1,sales_id);
			ps1.setInt(2,customer_id);
			ps1.setString(3,invoice_number);
			ps1.setDate(4,date);
			ps1.setTime(5,time);
			ps1.setFloat(6,total_amount) ;

			int x = ps1.executeUpdate() ;
			if(x>0)
			System.out.println("Record inserted in sales_invoice table");






		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());

			}

	}







void insertDetailsInSalesInvoiceDetails()
{

	try
	{

		int n = product_id.length ;
		while(n!=0)
		{

					String sql2 = "Insert into sales_invoice_details values(?,?,?,?,?)" ;
					PreparedStatement ps2 = cc.getStatement(sql2) ;

								ps2.setInt(1,sales_id);
								ps2.setInt(2,Integer.parseInt( product_id[n-1] ));
								ps2.setInt(3,Integer.parseInt( quantity[n-1] ));
								ps2.setFloat(4,Float.parseFloat( price[n-1] ));
								ps2.setFloat(5,Float.parseFloat( VAT[n-1] ));

								int y = ps2.executeUpdate() ;
								if(y>0)
					System.out.println("Record inserted in sales_invoice_details table");



					n-- ;
	}

	}
	catch(Exception e)
			{
				System.out.println(e.getMessage());

			}


}


ResultSet getProductDetailsOfSalesId(int sales_id)throws Exception
{


		String sql = "select * from sales_invoice_details where sales_id = ? " ;

		PreparedStatement ps = cc.getStatement(sql) ;

		ps.setInt(1,sales_id) ;
		return ps.executeQuery() ;

}


ResultSet getSalesDetailsBetweenDates(String startDate, String endDate) throws Exception
{
	String sql = "select * from sales_invoice where date between ? and ? " ;

	PreparedStatement ps = cc.getStatement(sql) ;

	ps.setString(1,startDate) ;
	ps.setString(2,endDate) ;

	return ps.executeQuery() ;


}


}